<template>
  <div>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="地址">
        <el-cascader size="large" :options="pcaTextArr" v-model="addressArray">
        </el-cascader>
      </el-form-item>
      <el-form-item label="部门">
        <el-select
          v-model="form.departmentId"
          placeholder="请选择部门"
          style="width: 150px"
        >
          <el-option label="生产部" :value="1" />
          <el-option label="采购部" :value="2" />
          <el-option label="管理部" :value="3" />
          <el-option label="销售部" :value="4" />
          <el-option label="大客户部" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio :value="true">男</el-radio>
          <el-radio :value="false">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="详细地址">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="upduser()">修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { axios } from "@/axios/axiosHelp";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import {
  provinceAndCityData,
  pcTextArr,
  regionData,
  pcaTextArr,
  codeToText,
} from "element-china-area-data";
const router = useRouter();
const route = useRoute();
const form = reactive({
  userId: route.query.id,
  name: "",
  pass: "123456",
  departmentId: "",
  sex: true,
  province: "",
  city: "",
  county: "",
  address: "",
  createUser: "",
  createTime: "",
  updateUser: "",
  updateTime: "",
  isDeleted: false,
});
const addressArray: any = ref([]);
const getusers = () => {
  axios
    .get("https://localhost:7081/api/User/GetUsers", {
      params: {
        userId: form.userId,
      },
    })
    .then((res) => {
      console.log(res.data.data,'555555');
      form.name = res.data.data.name;
      form.sex = res.data.data.sex;
      form.province = res.data.data.province;
      form.city = res.data.data.city;
      form.departmentId = res.data.data.departmentId;
      form.county = res.data.data.county;
      form.address = res.data.data.address;
      form.createUser = res.data.data.createUser;
      form.createTime = res.data.data.createTime;
      form.updateUser = res.data.data.updateUser;
      form.updateTime = res.data.data.updateTime;
      form.isDeleted = res.data.data.isDeleted;
      addressArray.value.push(
        res.data.data.province,
        res.data.data.city,
        res.data.data.county
      );
    });
};

const upduser = () => {
  if (form.name == "") {
    ElMessage.error("用户名不得为空");
    return;
  }
  form.province = addressArray.value[0];
  form.city = addressArray.value[1];
  form.county = addressArray.value[2];
  axios.post("https://localhost:7181/api/User/UpdUser", form).then((res) => {
    if (res.data.code == 100) {
      ElMessage.success(res.data.msg);
      router.push("/GetUser");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

onMounted(() => {
  getusers();
});
</script>

<style scoped>
</style>